﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp1
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<CulvertItem> Items { get; set; }
        private CulvertItem _selectedItem;

        public CulvertItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                OnPropertyChanged(nameof(SelectedItem));
            }
        }

        public ICommand AddItemCommand { get; set; }
        public ICommand RemoveItemCommand { get; set; }
        public ICommand SubmitCommand { get; set; }
        public ICommand RenameCommand { get; set; }

        public MainViewModel()
        {
            Items = new ObservableCollection<CulvertItem>
            {
                new CulvertItem { Name = "Culvert 1", Diameter = "1000", NumBarrel = "1" },
                new CulvertItem { Name = "Culvert 2", Diameter = "1000", NumBarrel = "1" }
            };

            SelectedItem = Items[0];

            AddItemCommand = new RelayCommand(AddItem);
            RemoveItemCommand = new RelayCommand(RemoveItem);
            SubmitCommand = new RelayCommand(Submit);
            RenameCommand = new RelayCommand(RenameItem);
        }

        private void AddItem(object obj)
        {
            Items.Add(new CulvertItem { Name = "New Item", Diameter = "600", NumBarrel = "0" });
        }

        private void RemoveItem(object obj)
        {
            if (SelectedItem != null)
                Items.Remove(SelectedItem);
        }

        private void Submit(object obj)
        {
            foreach (var item in Items)
            {
                item.UpdateParameter8();
            }
        }

        private void RenameItem(object obj)
        {
            if (obj is CulvertItem item)
            {
                string newName = Prompt.ShowDialog("Enter new name:", "Rename Item");
                if (!string.IsNullOrEmpty(newName))
                {
                    item.Name = newName;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public static class Prompt
    {
        public static string ShowDialog(string text, string caption)
        {
            Window prompt = new Window
            {
                Width = 300,
                Height = 150,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                Title = caption
            };
            StackPanel stackPanel = new StackPanel();
            TextBlock textBlock = new TextBlock { Text = text, Margin = new Thickness(10) };
            TextBox textBox = new TextBox { Margin = new Thickness(10) };
            Button button = new Button { Content = "OK", Margin = new Thickness(10) };

            button.Click += (sender, e) => { prompt.DialogResult = true; prompt.Close(); };

            stackPanel.Children.Add(textBlock);
            stackPanel.Children.Add(textBox);
            stackPanel.Children.Add(button);

            prompt.Content = stackPanel;
            prompt.ShowDialog();
            return textBox.Text;
        }
    }
}
